package com.bjsxt.user.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.common.util.PageInfoUtil;
import com.bjsxt.user.dao.IAdminDao;
import com.bjsxt.user.pojo.Admin;
import com.bjsxt.user.service.IUserService;

/*
 * userService接口的实现类
 * */
@Service("userService")
public class UserServiceImpl implements IUserService
{
	@Resource
	private IAdminDao adminDao;
	
	//查询单个的管理员
	@Override
	public Admin findOneAdmin(Map<String, Object> condMap)
	{
		// TODO Auto-generated method stub
		return this.adminDao.findOne(condMap);
	}

	//添加单个管理员
	@Override
	public JSONObject insertOneAdmin(Admin admin)
	{
		JSONObject resultJson=new JSONObject();
		int res = this.adminDao.insertOne(admin);
		if(res>0)
		{
			resultJson.put("code", 1);
			resultJson.put("info", "添加管理员成功");
			
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", admin.getId());
			
			resultJson.put("date", dataJson);
		}else
		{
			resultJson.put("code", 0);
			resultJson.put("info", "添加管理员失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject updateOneAdmin(Admin admin)
	{
		JSONObject resultJson=new JSONObject();
		int res = this.adminDao.updateOne(admin);
		if(res>0)
		{
			resultJson.put("code", 1);
			resultJson.put("info", "更新管理员成功");
			
		}else
		{
			resultJson.put("code", 0);
			resultJson.put("info", "更新管理员失败");
		}
		return resultJson;
	}

	@Override
	public List<Admin> findListAdmin(PageInfoUtil pageInfo,Map<String, Object> condMap)
	{
		//先判断是否分页
		if(pageInfo!=null)
		{
			//先查询总记录数
			condMap.put("pageCount", true);
			if(condMap.get("keyword")!=null){
				condMap.put("keyword", "%"+condMap.get("keyword")+"%");
			}
			List<Admin> adminLi = this.adminDao.findCondList(condMap);
			int totalRe = adminLi.get(0).getId();
			
			//准备分页查询的数据
			condMap.put("pageCount", false);
			pageInfo.setTotalRecord(totalRe);
			int currentRecord = pageInfo.getCurrRecord();
			int pageSize = pageInfo.getPageSize();
			condMap.put("currentRecord", currentRecord);
			condMap.put("pageSize", pageSize);
			//查询分页数据
			List<Admin> adminList = this.adminDao.findCondList(condMap);
			return adminList;
		}else
		{
			//直接准备不分页查询数据
			condMap.put("keyword", "%"+condMap.get("keyword")+"%");
			List<Admin> adminList = this.adminDao.findCondList(condMap);
			return adminList;
		}
		
	}
	
	
}
